package pool.dbpool ;

import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;

/**
 * @author 80002165 @date 2017年7月17日 下午4:40:46
 */
public class TestMain {
    static String driverName = "com.mysql.jdbc.Driver" ;
    static String url = "jdbc:mysql://127.0.0.1:3306/shop" ;
    static String user = "root" ;
    static String password = "root" ;
    
    public static void main(String[] args) {
        
        // 直接用connection来获取connection
//         test1() ;
        try {
            test2() ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    private static void test1() {
        // 直接用connection来获取connection
        long begintime = System.currentTimeMillis() ;
        for (int i = 0; i < 50; i++) {
            Connection connection = null ;
            try {
                connection = DBConnectionUtil.getConnection(driverName, url, user, password) ;
                getResult(connection, i, "直接取数据库连接") ;
            } catch (Exception e) {
                e.printStackTrace() ;
            } finally {
                if (connection != null) {
                    try {
                        connection.close() ;
                    } catch (SQLException e) {
                        e.printStackTrace() ;
                    }
                }
                
            }
        }
        
        System.out.println("测试共用时：" + (System.currentTimeMillis() - begintime)) ;
    }
    
    private static void test2() throws SQLException {
        // 直接用connection来获取connection
        long begintime = System.currentTimeMillis() ;
        ConnectionPool pool = new ConnectionPool(driverName, url, user, password) ;
        pool.setInitialConnections(10) ;
        pool.createPool() ;
        for (int i = 0; i < 50; i++) {
            Connection connection = null ;
            connection = pool.getConnection() ;
            getResult(connection, i, "连接池取数据库连接") ;
            pool.releaseConnection(connection);
        }
        
        System.out.println("测试共用时：" + (System.currentTimeMillis() - begintime)) ;
    }
    
    private static void getResult(Connection connection, int index, String info) {
        PreparedStatement statement = null ;
        ResultSet resultSet = null ;
        try {
            statement = connection.prepareStatement("select * from mmall_user where username='admin' ") ;
            resultSet = statement.executeQuery() ;
            while (resultSet.next()) {
//                System.out.println(info + "第" + index + "个结果：" + resultSet.getString("password")) ;
            }
        } catch (SQLException e) {
            e.printStackTrace() ;
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close() ;
                } catch (SQLException e) {
                    e.printStackTrace() ;
                }
            }
            
            if (statement != null) {
                try {
                    statement.close() ;
                } catch (SQLException e) {
                    e.printStackTrace() ;
                }
            }
            
        }
    }
}
